package Client;
import java.awt.*;
import java.awt.event.*;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.*;

import Models.Point2D;
import Models.StandardPoint2D;
import TechStuff.Client;
import TechStuff.ClientStarter;
import TechStuff.ServerStub;

public class ChessGame implements ActionListener{
	
	private JFrame frame;
	private JPanel board;
	private PiecesContainer piecesContainer;
	private JLabel[][] pieceLabels;
	private HashMap<JLabel, Point2D> labelCoordinateMap = new HashMap<JLabel, Point2D>();
	private ServerStub server;
	private Client client;
	private int id;
	private Point2D firstClick = null;
	
	
	public ChessGame() throws RemoteException {
		
		server = new ServerStub();
		client = new Client(this);
		id = server.registerClient();
		new ClientStarter(id, client);
		server.createClient(id);
		
		if (id == 1) {
			server.resetGame();
		}
		
		
		piecesContainer = PiecesContainer.getInstance();
		pieceLabels = new JLabel[8][8];
		makeFrame();
		
		
	}
	
	private void makeFrame() {
		frame = new JFrame("player" + id);
		makeMenuBar(frame);
		
	
		Container contentPane = frame.getContentPane();
		contentPane.setLayout(new BorderLayout());
		
		makeBoard();
		contentPane.add(board, BorderLayout.CENTER);
		
		JLabel player1 = new JLabel("Player1");
		JLabel player2 = new JLabel("Player2");
		player1.setForeground(Color.green);
		JPanel playerField = new JPanel(new GridLayout(2,2));
		contentPane.add(playerField,BorderLayout.EAST);
		playerField.add(player1);
		playerField.add(player2);
		
		frame.pack();
		frame.setVisible(true);
	}
	
	private void makeBoard(){
		board = new JPanel();
		board.setLayout(new GridLayout(8, 8));
		resetBoard();
		
	}
	
	private void resetBoard() {
	
		ImageIcon white = new ImageIcon("Images/White.gif");
		ImageIcon black = new ImageIcon("Images/Black.gif");
		
		for (int x = 0; x < 8; x++){
			for (int y = 0; y < 8; y++) {
				// set black and white grid
				if ((x + y) % 2 == 0) {
					createLabel(black, x, y);
				}
				else {
					createLabel(white, x, y);
				}
			}
		}
		
		resetPieces();
	}
	
	public void createLabel(ImageIcon icon, int x, int y)
	{
		final JLabel label = new JLabel(icon);
		label.addMouseListener(new MouseListener(){

			@Override
			public void mouseClicked(MouseEvent arg0) {
				Point2D click = labelCoordinateMap.get(label);
				System.out.println("Player " + id + " clicked " + click.getX() + "," + click.getY());
				if (firstClick == null) {
					System.out.println("And first click is null");
					firstClick = click;
				}
				else if(firstClick == click)
				{
					System.out.println("And first click is " + firstClick.getX() + "," + firstClick.getY());
					firstClick = null;
				}
				else {
					System.out.println("And first click is " + firstClick.getX() + "," + firstClick.getY());
					try {
						if (server.movePiece(firstClick, click, id)) {
							firstClick = null;
						}
					} catch (RemoteException e) {
						
						e.printStackTrace();
					}
				}
				
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		board.add(label, x, y);
		pieceLabels[y][x] = label;
		labelCoordinateMap.put(label, new StandardPoint2D(y,x));
		
	}
	
	public void movePiece(Point2D from, Point2D to) {
		JLabel fromLabel = pieceLabels[from.getX()][from.getY()];
		JLabel toLabel = pieceLabels[to.getX()][to.getY()];
		toLabel.setIcon(fromLabel.getIcon());
		if ((from.getX() + from.getY()) % 2 == 0) {
			fromLabel.setIcon(new ImageIcon("Images/Black.gif"));
		}
		else {
			fromLabel.setIcon(new ImageIcon("Images/White.gif"));
		}
		
	}
	
	private void resetPieces() {
		
		pieceLabels[0][0].setIcon(piecesContainer.getPiece("whiteRookBlack"));
		pieceLabels[1][0].setIcon(piecesContainer.getPiece("whiteKnightWhite"));
		pieceLabels[2][0].setIcon(piecesContainer.getPiece("whiteBishopBlack"));
		pieceLabels[3][0].setIcon(piecesContainer.getPiece("whiteQueenWhite"));
		pieceLabels[4][0].setIcon(piecesContainer.getPiece("whiteKingBlack"));
		pieceLabels[5][0].setIcon(piecesContainer.getPiece("whiteBishopWhite"));
		pieceLabels[6][0].setIcon(piecesContainer.getPiece("whiteKnightBlack"));
		pieceLabels[7][0].setIcon(piecesContainer.getPiece("whiteRookWhite"));
		
		pieceLabels[0][1].setIcon(piecesContainer.getPiece("whitePawnWhite"));
		pieceLabels[1][1].setIcon(piecesContainer.getPiece("whitePawnBlack"));
		pieceLabels[2][1].setIcon(piecesContainer.getPiece("whitePawnWhite"));
		pieceLabels[3][1].setIcon(piecesContainer.getPiece("whitePawnBlack"));
		pieceLabels[4][1].setIcon(piecesContainer.getPiece("whitePawnWhite"));
		pieceLabels[5][1].setIcon(piecesContainer.getPiece("whitePawnBlack"));
		pieceLabels[6][1].setIcon(piecesContainer.getPiece("whitePawnWhite"));
		pieceLabels[7][1].setIcon(piecesContainer.getPiece("whitePawnBlack"));
	
		pieceLabels[0][6].setIcon(piecesContainer.getPiece("blackPawnBlack"));
		pieceLabels[1][6].setIcon(piecesContainer.getPiece("blackPawnWhite"));
		pieceLabels[2][6].setIcon(piecesContainer.getPiece("blackPawnBlack"));
		pieceLabels[3][6].setIcon(piecesContainer.getPiece("blackPawnWhite"));
		pieceLabels[4][6].setIcon(piecesContainer.getPiece("blackPawnBlack"));
		pieceLabels[5][6].setIcon(piecesContainer.getPiece("blackPawnWhite"));
		pieceLabels[6][6].setIcon(piecesContainer.getPiece("blackPawnBlack"));
		pieceLabels[7][6].setIcon(piecesContainer.getPiece("blackPawnWhite"));
		
		pieceLabels[0][7].setIcon(piecesContainer.getPiece("blackRookWhite"));
		pieceLabels[1][7].setIcon(piecesContainer.getPiece("blackKnightBlack"));
		pieceLabels[2][7].setIcon(piecesContainer.getPiece("blackBishopWhite"));
		pieceLabels[3][7].setIcon(piecesContainer.getPiece("blackKingBlack"));
		pieceLabels[4][7].setIcon(piecesContainer.getPiece("blackQueenWhite"));
		pieceLabels[5][7].setIcon(piecesContainer.getPiece("blackBishopBlack"));
		pieceLabels[6][7].setIcon(piecesContainer.getPiece("blackKnightWhite"));
		pieceLabels[7][7].setIcon(piecesContainer.getPiece("blackRookBlack"));
		
	}
	
	
	
	private void makeMenuBar(JFrame frame)
    {
        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);
        
        // create the Game menu
        JMenu gameMenu = new JMenu("Game");
        menubar.add(gameMenu);
        
        JMenuItem newItem = new JMenuItem("New");
        newItem.addActionListener(this);
        gameMenu.add(newItem);
   
        JMenuItem quitItem = new JMenuItem("Quit");
        quitItem.addActionListener(this);
        gameMenu.add(quitItem);
        
        // create the Move menu
        JMenu moveMenu = new JMenu("Move");
        menubar.add(moveMenu);
        
        JMenuItem withDarwItem = new JMenuItem("WithDraw");
        withDarwItem.addActionListener(this);
        moveMenu.add(withDarwItem);
        
        // create the Help menu
        JMenu helpMenu = new JMenu("Help");
        menubar.add(helpMenu);
        
        JMenuItem helpItem = new JMenuItem("Help");
        helpItem.addActionListener(this);
        helpMenu.add(helpItem);
    }

	@Override
	public void actionPerformed(ActionEvent event) {
		
		
	}

}
